/*
 * ====================================================================
 * Copyright (c) 2002-2018 The RapidSVN Group.  All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program (in the file GPL.txt).
 * If not, see <http://www.gnu.org/licenses/>.
 *
 * This software consists of voluntary contributions made by many
 * individuals.  For exact contribution history, see the revision
 * history and logs, available at http://rapidsvn.tigris.org/.
 * ====================================================================
 */
#ifndef _MAIN_FRAME_HEADER_H_INCLUDED_
#define _MAIN_FRAME_HEADER_H_INCLUDED_

// wxWidgets
#include "wx/frame.h"
#include "wx/textctrl.h"
#include "wx/toolbar.h"

// svncpp
#include "svncpp/targets.hpp"

// app
#include "action_worker.hpp"
#include "tracer.hpp"
#include "log_action.hpp"
#include "rapidsvn_generated.h"
#include "utils.hpp"

typedef enum
{
  ACTIVEPANE_FOLDER_BROWSER,
  ACTIVEPANE_FILELIST
}
ActivePane;

// forward declarations
class wxFrame;
class wxMenu;
class wxString;
class wxListEvent;

class MainFrame : public MainFrameBase
{
public:
  /**
   * Constructor that will be called by the App
   *
   * @param title Application title
   * @param locale The locale that's used by the app
   */
  MainFrame(const wxString & title,
            const wxLocale & locale);
  virtual ~MainFrame();

  /** Allow children to trim their popup menus using the frames logic */
  void
  TrimDisabledMenuItems(wxMenu & menu);

  /**
  * Change the active window, if the corresponding event is handled
  * in one of the sub-windows
  */
  void
  SetActivePane(ActivePane value);

private:
  /** disallow default constructor */
  MainFrame();

  /** disallow copy constructor */
  MainFrame(const MainFrame &);

  void OnFocusChanged(wxCommandEvent & event);

  // File menu
  void OnAddWcBookmark(wxCommandEvent & event);
  void OnAddRepoBookmark(wxCommandEvent & event);
  void OnRemoveBookmark(wxCommandEvent & event);
  void OnEditBookmark(wxCommandEvent & event);
  void OnQuit(wxCommandEvent & event);

  // View menu
  void OnPreferences(wxCommandEvent & event);
  void OnRefresh(wxCommandEvent & event);
  void OnColumnReset(wxCommandEvent & event);
  void OnColumn(wxCommandEvent & event);
  void OnIncludePath(wxCommandEvent & event);
  void OnSortAscending(wxCommandEvent & event);
  void OnColumnSorting(wxCommandEvent & event);
  void OnFlatView(wxCommandEvent & event);
  void OnRefreshWithUpdate(wxCommandEvent & event);
  void OnShowUnversioned(wxCommandEvent & event);
  void OnShowUnmodified(wxCommandEvent & event);
  void OnShowModified(wxCommandEvent & event);
  void OnShowConflicted(wxCommandEvent & event);
  void OnIgnoreExternals(wxCommandEvent & event);
  void OnShowIgnored(wxCommandEvent & event);
  void OnStop(wxCommandEvent & event);
  void OnIndicateModifiedChildren(wxCommandEvent & event);

  // Query menu
  void OnLog(wxCommandEvent & event);
  void OnInfo(wxCommandEvent & event);

  // Modify menu
  void OnFileCommand(wxCommandEvent & event);

  // Extras menu
  void OnCleanup(wxCommandEvent & event);

  // Help menu
  void OnHelpContents(wxCommandEvent & event);
  void OnHelpIndex(wxCommandEvent & event);
  void OnHelpStartupTips(wxCommandEvent & event);
  void OnAbout(wxCommandEvent & event);

  // Test menu
  void OnTestNewWxString(wxCommandEvent & event);
  void OnTestListener(wxCommandEvent & event);
  void OnTestCheckout(wxCommandEvent & event);
  void PrintTimeMeasurements(apr_time_t start, apr_time_t end,
                             const wxString & name);
  void OnTestCertDlg(wxCommandEvent & event);
  void OnTestDestinationDlg(wxCommandEvent & event);
  void OnTestDndDlg(wxCommandEvent & event);

  // toolbar events
  void OnToolEnter(wxCommandEvent & event);

  // log toolbar events
  void OnLogClear(wxCommandEvent & event);
  void OnLogToggle(wxCommandEvent & event);
  void OnLogUpdate(wxUpdateUIEvent & event);
  // Events from action threads
  void OnActionEvent(wxCommandEvent & event);
  void OnListenerEvent(wxCommandEvent & event);

  // Bookmarks
  void AddWcBookmark();
  void AddRepoBookmark();
  void RemoveBookmark();
  void EditBookmark();

  // Authentication
  void OnLogin(wxCommandEvent & event);
  void OnLogout(wxCommandEvent & event);

  // Folder Browser event handlers
  void OnFolderBrowserSelChanged(wxTreeEvent & event);
  void OnFolderBrowserKeyDown(wxTreeEvent & event);

  // File List event handlers
  void OnFileListSelected(wxListEvent & event);

  // Log list event handlers
  void OnLogListBrowse(wxCommandEvent & event);
  void OnLogListDiff(wxCommandEvent & event);


  /**
   * Invoke the default action on the currently selected item(s)
   *
   * For folders, this is to open them within RapidSVN. For files, this is to
   * activate the default external editor.
   *
   * @return false if no items were found to invoke default action on
   */
  bool InvokeDefaultAction();

  /**
   * Update the contents of the filelist
   */
  void RefreshFileList();

  /**
   * Update the contents of the folder browser
   */
  void RefreshFolderBrowser();

  // Auxiliary functions
  void UpdateCurrentPath();
  void UpdateMenuSorting();
  void UpdateMenuIncludePath();
  void UpdateMenuAscending();
  void SetIncludePathVisibility(bool flatMode);

  // Enable/disable action menu items
  void OnUpdateCommand(wxUpdateUIEvent & updateUIEvent);

  // Utility functions
  void ShowLog();
  void ShowInfo();

  void DelEntries();
  void MakeRevert();
  void MakeResolve();

  void MakeCopy();
  void Rename();
  void Mkdir();
  void Merge();
  void Contents();
  void ShowPreferences();

  void Perform(Action * action);

  void OnIdle(wxIdleEvent& event);
protected: // inherited from MainFrameBase
  virtual void OnActivate(wxActivateEvent & event);
  virtual void OnSize(wxSizeEvent & sizeEvent);
  virtual void OnFolderBrowserSetFocus(wxFocusEvent& event);
  virtual void OnListCtrlSetFocus(wxFocusEvent& event);
private:
  /** hide implementation data */
  struct Data;
  Data * m;

  ActionWorker * m_actionWorker;

  wxString m_title;
  svn::Context * m_context;

private:
  DECLARE_EVENT_TABLE()
};

const int ID_TOOLBAR = 500;

#endif
/* -----------------------------------------------------------------
 * local variables:
 * eval: (load-file "../rapidsvn-dev.el")
 * end:
 */
